Repository navigation
Conversation
Ruby cannot distinguish `obj.attr` from `obj.attr()`, so PyCall used to call every callable attribute. This broke objects like scipy.stats.norm, a callable instance of norm_gen: `stats.norm.pdf(x, 0, 10)` became `stats.norm().pdf(x, 0, 10)` and raised TypeError (red-data-toolsGH-87). Now, when no argument is given, a callable attribute is called only if it is a routine in the sense of Python's inspect.isroutine(): a builtin function or method, a bound method, a method-wrapper, or a non-data descriptor (Python functions, method descriptors, Cython functions, functools.lru_cache wrappers, numpy's array function dispatchers, etc.). functools.partial objects, and Ruby Proc/Method objects stored in Python attributes, are also treated as routines. Other callable objects, such as instances of classes defining __call__ and numpy ufuncs, are returned as they are. Calls with arguments are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
@kou I agree with you, but we might need to bump the version to 2.0.0 when we release this change. |
Member
|
OK. Let's use "2.0.0" for the next version. |
This was referenced Nov 17, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #87.
Problem
PyCall.import_module('scipy.stats').norm.pdf([1, 2, 3], 0, 10)raiseswhile the same expression works in Python.
scipy.stats.normis not a class or a function but a callable instance ofnorm_gen. Ruby cannot distinguishstats.normfromstats.norm(), andpycall_pyobject_wrapper_wrapper_methodcalls every callable attribute, sostats.norm.pdf(x, 0, 10)is evaluated asstats.norm().pdf(x, 0, 10):pdfof a frozen distribution (loc=0, scale=1), which only takesx.Change
When an attribute is accessed without arguments, it is called only if it is a routine. Otherwise the callable object is returned as is. Calls with arguments are unchanged.
"Routine" follows Python's own definition in
inspect.isroutine(), implemented in C with type checks (no Python-level call per access):builtin_function_or_method, boundmethod, andmethod-wrapper(and subtypes)__get__but not__set__. This covers Python functions, method descriptors, slot wrappers, Cython functions (cython_function_or_method),functools.lru_cachewrappers, numpy's_ArrayFunctionDispatcher(np.sum, etc.), and so on.Two additions to keep existing code working:
functools.partial: it is a routine on Python 3.14+ (it gained__get__), and it is special-cased so that the behavior does not depend on the Python version.Proc/Methodobjects stored in a Python attribute are still called, as before.Class objects are still returned as is, as before.
Behavior change
With no arguments, these are now returned instead of being called:
scipy.stats.normand other rv instancesnp.sin,scipy.special.gamma)TypeError__call__,operator.itemgetter(...)TypeError: non-callable ruby objectEverything else is unchanged. I checked these against real numpy/scipy:
np.get_include,np.random.random,np.random.default_rng(Cython),ndarray.sum/copy/tolist, staticmethods and classmethods are all still called without arguments.Risk: code that relies on
obj.callable_instancecalling it with zero arguments now gets the object back. It can useobj.callable_instance.()(or.call) instead. The README section "The callable attribute of an object" now documents this.Tests
spec/pycall/callable_attribute_spec.rbwith a scipy-like helper modulespec/python/pycall/callable_attribute.py(no scipy needed). It covers the callable-instance case (no args, positional args, keyword args,.(), class/instance attributes). It also checks that zero-argument functions, bound methods, staticmethods, classmethods, builtin methods and functions, method-wrappers,lru_cachewrappers,partialobjects, callable non-data descriptors, and Ruby lambdas are still called.Not included
pyfrom ..., import: :norm(lib/pycall/import.rb) still defines a method that calls any non-class callable, so the same issue remains there.🤖 Generated with Claude Code